| Total Complexity | 7 |
| Total Lines | 36 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import { BoostHistory, BoostHistoryItem } from '@/types/BoostHistory'; |
||
| 3 | |||
| 4 | export class HistoryManager { |
||
| 5 | public data: BoostHistory = []; |
||
| 6 | |||
| 7 | constructor(public filename: string) { |
||
| 8 | this.load(); |
||
| 9 | } |
||
| 10 | |||
| 11 | public for(boostName: string): BoostHistory { |
||
| 12 | return this.data.filter(item => item.boost === boostName); |
||
| 13 | } |
||
| 14 | |||
| 15 | public createEntry(item: BoostHistoryItem): BoostHistoryItem { |
||
| 16 | this.data.push(item); |
||
| 17 | |||
| 18 | return new Proxy(this.data[this.data.length - 1], {} as any); |
||
| 19 | } |
||
| 20 | |||
| 21 | public save() { |
||
| 22 | if (this.filename.length === 0) { |
||
| 23 | return; |
||
| 24 | } |
||
| 25 | |||
| 26 | writeFileSync(this.filename, JSON.stringify(this.data), { encoding: 'utf8' }); |
||
| 27 | } |
||
| 28 | |||
| 29 | public load() { |
||
| 30 | if (this.filename.length === 0) { |
||
| 31 | return; |
||
| 32 | } |
||
| 33 | |||
| 34 | if (!existsSync(this.filename)) { |
||
| 35 | this.save(); |
||
| 36 | } |
||
| 37 | |||
| 38 | this.data = JSON.parse(readFileSync(this.filename, { encoding: 'utf8' })); |
||
| 39 | } |
||
| 41 |